﻿Imports System.Web
Imports System.ServiceModel.Syndication
Imports System.Xml


Public Class PDCListMaker

	Private Function BuildListAgilityPack(ByVal CatalogURL As String) As String

		Dim htmWeb As New HtmlAgilityPack.HtmlWeb

		Dim htmDoc As HtmlAgilityPack.HtmlDocument = htmWeb.Load(CatalogURL)

		Dim links As HtmlAgilityPack.HtmlNodeCollection = htmDoc.DocumentNode.SelectNodes("//div[@class='title']")

		Dim ls = New Generic.List(Of Session)

		For Each link As HtmlAgilityPack.HtmlNode In links
			Dim title = link.InnerText.Substring(link.InnerText.IndexOf(" "c) + 1, link.InnerText.Length - 1 - link.InnerText.IndexOf(" "c))
			Dim code = link.InnerText.Substring(0, link.InnerText.IndexOf(" "c))

			ls.Add(New Session() With {.Code = code, .Title = title})

		Next
		Return BuildHTML(ls)

	End Function

	Private Function BuildListRSS(ByVal RSSFeedURL As String) As String
		Dim reader As XmlReader = XmlReader.Create(RSSFeedURL)
		Dim formatter As New Rss20FeedFormatter()

		If formatter.CanRead(reader) = False Then
			Return ""

		End If

		formatter.ReadFrom(reader)
		reader.Close()
		Dim ls = New Generic.List(Of Session)

		For Each feed In formatter.Feed.Items
			'Dim x As XElement = XElement.Parse(feed.ElementExtensions(5).GetReader().ReadOuterXml)

			If feed.Id.EndsWith("/") Then
			Else
				Dim code As String = feed.Id.Substring(feed.Id.LastIndexOf("/"c) + 1, feed.Id.LastIndexOf("."c) - 1 - feed.Id.LastIndexOf("/"c))
				ls.Add(New Session() With {.Title = feed.Title.Text, .Code = code})

			End If

		Next

		Return BuildHTML(ls)

	End Function

	Private Function BuildHTML(ByVal ls As Generic.List(Of Session)) As String
		Dim q = From link In ls Order By link.SortTitle

		Dim sb = New System.Text.StringBuilder
		sb.Append("<html><body>")
		sb.Append("<table>")
		sb.Append(GetHeader())

		For Each link As Session In q
			sb.Append("<tr>")

			CurrentSessionLabel.Text = link.Code
			CurrentSessionLabel.Refresh()

			If Not link.Code.Contains("-R") Then
				sb.Append("<td>")
				sb.Append(GetTitle(link.SortTitle, link.Code, VerifyLinkCheckBox.Checked))

				sb.Append("</td>")
				sb.AppendLine(GetVideoLinks(link.Code, VerifyLinkCheckBox.Checked))
			End If

			sb.Append("</tr>")

		Next
		sb.Append("</table>")

		sb.Append("</body></html>")


		Return sb.ToString

	End Function

	Private Sub BuildListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildListButton.Click
		EnableUI(False)

		Dim html As String = ""
		If MicrosoftPDCURLRadioButton.Checked Then
			html = BuildListAgilityPack(CatalogTextBox.Text)
		ElseIf RSSRadioButton.Checked Then
			html = BuildListRSS(RSSTextBox.Text)
		Else
			html = BuildListRSS("cachedfeed.xml")

		End If

		WebBrowser1.DocumentText = html
		CurrentSessionLabel.Text = ""

		EnableUI(True)

	End Sub

	Private Sub EnableUI(ByVal enable As Boolean)
		CatalogTextBox.Enabled = enable
		RSSTextBox.Enabled = enable

		BuildListButton.Enabled = enable

		VerifyLinkCheckBox.Enabled = enable
		WMVCheckBox.Enabled = enable
		WMVHQCheckBox.Enabled = enable
		ZuneCheckBox.Enabled = enable
		MP4CheckBox.Enabled = enable
		ZIPCheckBox.Enabled = enable
		PPTXCheckBox.Enabled = enable
		CopyLinkLabel.Enabled = enable

		DataSourceGroupBox.Enabled = enable

	End Sub

	Private Function GetTitle(ByVal title As String, ByVal code As String, ByVal verifyLink As Boolean) As String
		'http://channel9.msdn.com/pdc2008/TL42/
		Dim sb As New System.Text.StringBuilder

		Dim url = "http://channel9.msdn.com/pdc2008/" & code

		If PageExists(url, verifyLink) Then
			sb.Append("<a href=")
			sb.Append(Chr(34))
			sb.Append(url)
			sb.Append(Chr(34))
			sb.Append(">")
			sb.Append(title)
			sb.Append("</a>")
		Else
			sb.Append(title)
		End If

		Return sb.ToString

	End Function

	Private Function GetVideoLinks(ByVal code As String, ByVal verifyLink As Boolean) As String


		Dim sb = New System.Text.StringBuilder

		If WMVHQCheckBox.Checked Then
			sb.AppendLine(GetResourceLink("http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/", code, "wmv", "WMV-HQ", verifyLink))
		End If
		If WMVCheckBox.Checked Then
			sb.AppendLine(GetResourceLink("http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV/", code, "wmv", "WMV", verifyLink))
		End If
		If ZuneCheckBox.Checked Then
			sb.AppendLine(GetResourceLink("http://mschnlnine.vo.llnwd.net/d1/pdc08/ZUNE/", code, "wmv", "Zune", verifyLink))
		End If
		If MP4CheckBox.Checked Then
			sb.AppendLine(GetResourceLink("http://mschnlnine.vo.llnwd.net/d1/pdc08/MP4/", code, "mp4", "MP4", verifyLink))
		End If
		If ZIPCheckBox.Checked Then
			sb.AppendLine(GetResourceLink("http://mschnlnine.vo.llnwd.net/d1/pdc08/ZIP/", code, "zip", "ZIP", verifyLink))
		End If
		If PPTXCheckBox.Checked Then
			sb.AppendLine(GetResourceLink("http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/", code, "pptx", "PPTX", verifyLink))
		End If

		Return sb.ToString
	End Function

	Private Function GetHeader() As String
		Dim sb = New System.Text.StringBuilder

		sb.Append("<thead>")
		sb.Append("<tr>")
		sb.Append("<th width=0>")
		sb.Append("Title")
		sb.Append("</th>")
		If WMVHQCheckBox.Checked Then
			sb.Append("<th width=75>")
			sb.Append("WMV-HQ")
			sb.Append("</th>")
		End If
		If WMVCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("WMV")
			sb.Append("</th>")
		End If
		If ZuneCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("Zune")
			sb.Append("</th>")
		End If
		If MP4CheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("MP4")
			sb.Append("</th>")
		End If
		If ZIPCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("ZIP")
			sb.Append("</th>")
		End If
		If PPTXCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("PPTX")
			sb.Append("</th>")
		End If


		sb.Append("</tr>")
		sb.Append("</thead>")

		Return sb.ToString

	End Function

	Private Function GetResourceLink(ByVal URLPrefix As String, ByVal code As String, ByVal extension As String, ByVal name As String, ByVal verifyLink As Boolean) As String
		Dim sb = New System.Text.StringBuilder
		Dim url = URLPrefix & code & "." & extension
		Dim url2 = URLPrefix & code & "." & extension.ToUpper
		Dim finalurl As String = ""

		Dim addlink As Boolean

		If PageExists(url, verifyLink) Then
			addlink = True
			finalurl = url
		Else
			addlink = False
		End If

		If addlink = False AndAlso PageExists(url2, verifyLink) Then
			addlink = True
			finalurl = url2
		End If

		sb.Append("<td>")

		If addlink Then
			sb.Append("<a href=")
			sb.Append(Chr(34))
			sb.Append(finalurl)
			sb.Append(Chr(34))
			sb.Append(">")
			sb.Append(name)
			sb.Append("</a>")

		Else
			sb.Append("N/A")
		End If
		sb.Append("</td>")

		Return sb.ToString


	End Function

	Private Function PageExists(ByVal URL As String, ByVal verifyLink As Boolean) As Boolean

		Return PageVerify.PageExists(URL, verifyLink)

	End Function


	Private Sub CopyLinkLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles CopyLinkLabel.LinkClicked
		If WebBrowser1.DocumentText.Length > 0 Then
			My.Computer.Clipboard.SetText(WebBrowser1.DocumentText.Replace("<html><body>", "").Replace("</body></html>", ""))
		End If
	End Sub

	Private Sub MicrosoftPDCURLRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MicrosoftPDCURLRadioButton.CheckedChanged
		CatalogTextBox.ReadOnly = Not MicrosoftPDCURLRadioButton.Checked
	End Sub

	Private Sub RSSRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RSSRadioButton.CheckedChanged
		RSSTextBox.ReadOnly = Not RSSRadioButton.Checked
	End Sub

	Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		'These settings are manually bound to fix an issue where if I have them "really" bound 
		'  they would have a weird click behavior, where you would have to click them twice 
		My.Settings.RSSRadioButton = RSSRadioButton.Checked
		My.Settings.MicrosoftPDCURLRadioButton = MicrosoftPDCURLRadioButton.Checked
		My.Settings.CachedRSSRadioButton = CachedRSSRadioButton.Checked

	End Sub

	Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'These settings are manually bound to fix an issue where if I have them "really" bound 
		'  they would have a weird click behavior, where you would have to click them twice 
		RSSRadioButton.Checked = My.Settings.RSSRadioButton
		MicrosoftPDCURLRadioButton.Checked = My.Settings.MicrosoftPDCURLRadioButton
		CachedRSSRadioButton.Checked = My.Settings.CachedRSSRadioButton


	End Sub
End Class
